package com.koron.css2.clientCenter.impl;

import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.bean.UserReuseBean;
import com.koron.css2.clientCenter.mapper.ChangeUserMapper;
import com.koron.css2.clientCenter.mapper.ReuseUserMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.meterService.bean.MsReuseBean;
import com.koron.css2.meterService.mapper.MsReuseMapper;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.workflow.ProcessInterface;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.text.SimpleDateFormat;

/**
 * 复用审批处理
 * 
 * @author 甄玉晴
 *
 */
public class ReuseUserWorkFlow implements ProcessInterface {

	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, int operation) {
		MessageBean<?> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", Void.class);
		try {
			// 审批后执行
			if("approval".equalsIgnoreCase(fromNodeCode) && 32 == operation) {
				// 获取复用的单据信息
				ReuseUserMapper mapper = factory.getMapper(ReuseUserMapper.class);
				UserReuseBean bean = mapper.getUserReuseInfoById(billId);
				// 修改用户信息
				UserInfoMapper userMapper = factory.getMapper(UserInfoMapper.class);
				userMapper.userInfoStatusUpdate(1 + "", bean.getUserNo());
				//获取用户信息
				UserBean user=userMapper.getUserInfoByUserNo(bean.getUserNo(),UserBean.USER);
				
				//更新变更事项到正式表
				ChangeUserMapper cmapper = factory.getMapper(ChangeUserMapper.class);
				cmapper.userChangeItemfromTemp(billId);
				// 如果需要复表则需要生成一个复表单
				if (bean.getReuseMeterFlag() == 1) {
					//填写复表单
					MsReuseBean msBean = addMsReusePlan(factory,user,bean,userInfo);
					//启用水表复表流程
					/*
					ProcessStartBean processStart = new ProcessStartBean();
					processStart.setBillId(msBean.getId());
					processStart.setTitle("复表审批"+"("+msBean.getBillNo()+")");
					processStart.setOperator(userInfo.getUserInfo().getAcount());
					processStart.setFormVariables(new HashMap<String, Object>());
					processStart.setReceiptType("client_reuse");
					processStart.setComment("复表审批");
					*/
//					JSONObject json = new JSONObject();
//					json.put("title", "复表审批"+"("+msBean.getBillNo()+")");
//					json.put("billId", msBean.getId());
//					json.put("operator", userInfo.getUserInfo().getAcount());
//					json.put("formVariables", new HashMap<String, Object>());
//					json.put("comment", "复表审批");
//					json.put("receiptType", "ms_reuse");
//					
//					RequestBean req = new RequestBean();
//					req.setData(json);				
//					ProcessCommit processCommit = new ProcessCommit();
//					info = processCommit.exec(factory, userInfo, req);
				}

			}
		} catch (Exception e) {
			factory.close(false);
			logger.error("复用审批后续操作失败", e);
			info = MessageBean.create(Constant.MESSAGE_INT_FAIL, "复用审批后续操作失败", Void.class);
		}
		return info;
	}

	/**
	 * 生成复表单据
	 * 
	 * @param factory
	 * @param user
	 * @param stopBean
	 * @param userInfo
	 */
	public MsReuseBean addMsReusePlan(SessionFactory factory, UserBean user, UserReuseBean reuseBean,
			UserInfoBean userInfo) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		// 获取拆表信息
		MsReuseBean bean = new MsReuseBean();
		try {
			bean.setBillDate(simpleDateFormat.parse(reuseBean.getBillDate()));
			bean.setBillNo(Tools.getBillNo(factory, "MS_REUSE"));
			bean.setMeterNo(user.getMeterNo());
			bean.setUserNo(user.getUserNo());
			bean.setCreateInfo(userInfo);
			MsReuseMapper msMapper = factory.getMapper(MsReuseMapper.class);
			// 添加单据信息
			msMapper.addMsReuse(bean);
		} catch (Exception e) {
			logger.error("复用审批后续操作增加复用单据失败", e);
			e.printStackTrace();
		}
		return bean;
	}
	 @Override
		public MessageBean<?> before(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, int operation) {
			return exec(factory, userInfo, billId, fromNodeCode, operation);
		}

		@Override
		public MessageBean<?> after(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, String toNodeCode) {
			return null;
		}

}
